<template>
    <div>
        <el-form ref="ruleFormRef" :inline="true" :model="ruleForm" class="demo-form-inline" label-width="100px"
            label-position="right" :rules="rules">
            <el-row v-if="showname" justify="center">
                <el-col :span="22" :offset="0">
                    <el-form-item label="学员姓名：" prop="comment">
                        <el-input v-model="ruleForm.name" readonly />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row justify="center">
                <el-col :span="22" :offset="0">
                    <el-form-item label="训练车型：" prop="comment">
                        <el-input v-model="ruleForm.carType" readonly />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row justify="center">
                <el-col :span="22" :offset="0">
                    <el-form-item label="所在校区：" prop="comment">
                        <el-select v-model="ruleForm.schoolName" disabled>
                            <el-option :label="schoolnameMap[ruleForm.schoolName]" :value="ruleForm.schoolName">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row justify="center">
                <el-col :span="22" :offset="0">
                    <el-form-item label="分配教练：" prop="coach">
                        <el-select v-model="ruleForm.coach" placeholder="请选择教练" clearable>
                            <el-option v-for="item in coachList" :label="item.name" :value="item.name"
                                :key="item.objectId" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row justify="center">
                <el-col :span="22" :offset="0">
                    <el-form-item label="备注：" prop="comment">
                        <el-input v-model="ruleForm.comment" placeholder="" clearable />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row justify="end">
                <el-col :span="6" :offset="0">
                    <el-form-item>
                        <el-button type="warning" @click="submitForm(ruleFormRef)">提交</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { examType, objType } from '../../../types/examType';
import { coachGet, stuExamBatchPut, stuExamInfoPut } from "../../../api/student"
import { coachType } from '../../../types/trarecord';
//父子通信，接收父组件传递的数据
interface propsType {
    assignRowData?: examType;
    schoolnameMap: objType;
    showname: number,
    selArr?: Array<string>
    tableData?: examType[]
    conditionData?: {
        carType: string;
        coach: string;
        object: string
        schoolName: string
    }
}
const props = defineProps<propsType>()
let ruleForm = reactive({}) as {
    name?: string;
    carType: string;
    coach: string;
    comment?: string;
    schoolName: string;
    objectId?: string;
}
//获取教练list
let coachList = ref<coachType[]>([])
let coachCondition = reactive<{
    carType: string;
    object: string;
    schoolName: string;
}>({
    carType: '',
    object: '',
    schoolName: '',
})
if (props.showname) {
    ruleForm = props.assignRowData!
    let { carType, schoolName } = props.assignRowData!
    coachCondition = { carType, object: props.assignRowData!.subject, schoolName: props.schoolnameMap[schoolName] }
} else {
    ruleForm = props.conditionData!
    let { carType, schoolName, object } = props.conditionData!
    coachCondition = { carType, schoolName, object }
}

//获取对应教练列表
let res = await coachGet(coachCondition)
coachList = res.data.results

//表单校验
let ruleFormRef = ref<FormInstance>()
const rules = reactive<FormRules>({
    coach: [
        { required: true, message: '请选择分配教练' },
    ],
})
let emit = defineEmits(['assignIsShow'])
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log("提交信息",ruleForm);
            if (props.showname) {
                let { objectId, coach } = ruleForm
                let obj = { objectId, coach }
                stuExamInfoPut(ruleForm.objectId!, obj).then(() => {
                    emit("assignIsShow",coach)
                })
            }else{
                let {coach}=ruleForm
                stuExamBatchPut(props.selArr!,{coach}).then(()=>{
                    emit("assignIsShow",coach)
                })
            }

        } else {
            console.log('error submit!', fields)
        }
    })
}
</script>
<style scoped></style>